<?php
namespace Admin\Controller;

class TopicController extends CommonController {
    public function index(){

        $user = D('topic');
        $get = $user->getTopic();
        $list = $get['data'];
        $page = $get['page'];
        // var_dump($list);
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->display();
    }

    public function topic_edit()
    {
        $id = I('get.id');
        $field='email,title,content,createtime';
        $topic = M('topic')->table('dota2_topic t,dota2_user u')->field($field)->where('t.uid=u.u_id and t.t_id='.$id)->select()[0];
        // var_dump($topic);
        $this->assign('data',$topic);
        $this->display();
    }

    public function topic_update()
    {
        $data = I('post.');
        $map['t_id'] = $_GET['id'];
        // var_dump($data);
        if (M('topic')->where($map)->save($data)) {
            $this->success('修改成功',U('Topic/index'));
        }else{
            $this->error('修改失败');
        }
    }

    public function topic_add()
    {
        $this->show();
    }

    public function topic_insert()
    {
        $user = D('topic');
        $uid = $user->checkuser($_POST['user']);
        if (!$uid) {
            $this->error('该作者不存在');
        }

        $data['title'] = $_POST['title'];
        $data['content'] = $_POST['content'];
        $data['uid'] = $uid;
        $data['createtime'] = time();
        $res = M('topic')->add($data);
        if ($res) {
            $this->success('发布成功',U('Topic/index'));
        }else{
            $this->error('发布失败');
        }
    }

    public function topic_del()
    {
        $tid = I('get.tid');
        $data['status'] = 0;
        $res = M('topic')->where('t_id='.$tid)->save($data);
        // echo json_encode($res);die;
        if ($res) {
            echo "1";
        }else{
            echo '0';
        }
    }

    public function topic_trash(){

        $user = D('topic');
        $get = $user->getBrash();
        $list = $get['data'];
        $page = $get['page'];
        // var_dump($list);
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->display();
    }

    public function topic_back()
    {
        $tid = I('get.tid');
        $data['status'] = 1;
        $res = M('topic')->where('t_id='.$tid)->save($data);
        // echo json_encode($res);die;
        if ($res) {
            echo "1";
        }else{
            echo '0';
        }
    }

    public function topic_die()
    {
        $tid = I('get.tid');
        $data['status'] = 0;
        $res = M('topic')->where('t_id='.$tid)->delete();
        $res1 = M('reply')->where('tid='.$tid)->delete();
        $res2 = M('reply_floor')->where('tid='.$tid)->delete();

        if ($res) {
            echo "1";
        }else{
            echo '0';
        }
    }

}